import 'package:decimal/decimal.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cwhisky/bean/good_entity.dart';
import 'package:flutter_cwhisky/common/style/images.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../bean/mall_home_entity.dart';
import '../../../common/style/colors.dart';
import '../../../store/user_state.dart';
import '../../image_view.dart';
import '../../text_view.dart';

class GoodComponent extends StatelessWidget {
  final GoodEntity good;
  bool showLabel = true;
  bool isKnight = UserState.isKnight;
  String price = '--';
  String orgPrice = '';
  bool showLine = false;
  int style = 0;
  double imageWidth = 90.r;
  double imageHeight = 90.r;

  GoodComponent({Key? key, required this.good, this.style = 0}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('build==${good.cnName}');
    _handlePrice();
    return Container(
      padding: EdgeInsets.only(left: 12.r, right: 12.r, top: 24.r),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 6.r),
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(6.r), color: AppColors.white),
            padding: EdgeInsets.symmetric(horizontal: 6.r, vertical: 2.r),
            child: Stack(
              alignment: Alignment.center,
              children: [
                ImageView(
                  url: good.image ?? '',
                  height: 144.r,
                ),
                good.balanceCount! <= 0
                    ? ImageView(
                        url: 'assets/images/icon_good_sold_out.png',
                        width: 40.r,
                        height: 40.r,
                      )
                    : const SizedBox(),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 14.r),
            child: TextView(
              text: good.enName ?? '',
              fontSize: 12.sp,
              fontColor: AppColors.color332B29,
              fontWeight: FontWeight.w700,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 5.r),
            child: TextView(
              text: good.cnName ?? '',
              fontSize: 12.sp,
              fontColor: AppColors.color332B29,
              fontWeight: FontWeight.w500,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 5.r),
            child: TextView(
              text: good.goodsCode??'',
              fontSize: 10.sp,
              fontColor: AppColors.color99,
              fontWeight: FontWeight.w400,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 21.r),
            child: Row(
              children: [
                Expanded(
                  child: TextView(
                    text: price,
                    fontColor: AppColors.color332B29,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.w900,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(4.r),
                  child: ImageView(
                    url: AppImages.tabCartNormal,
                    width: 16.r,
                    height: 16.r,
                  ),
                )
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(
              top: 6.r,
            ),
            child: Row(
              children: [
                TextView(
                  text: orgPrice,
                  fontColor: showLine ? AppColors.color99 : AppColors.color332B29,
                  fontSize: 12.sp,
                  fontWeight: showLine ? FontWeight.w400 : FontWeight.w500,
                  decoration: showLine ? TextDecoration.lineThrough : TextDecoration.none,
                ),
                showLabel
                    ? Container(
                        margin: EdgeInsets.only(left: 6.r),
                        width: 30.r,
                        height: 14.r,
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(color: AppColors.color332B29),
                        child: TextView(
                          text: '会员价',
                          fontSize: 8.sp,
                          fontColor: const Color(0xffEFD3A7),
                        ),
                      )
                    : const SizedBox(
                        width: 30,
                        height: 14,
                      )
              ],
            ),
          )
        ],
      ),
    );
  }

  _handlePrice() {
    //零售价
    var price = 0.0;
    var knightPrice = 0.0;
    if (good.showPriceType == 0) {
      price = good.price ?? 0.0;
    } else if (good.showPriceType == 1) {
      price = good.price ?? 0.00;
      knightPrice = good.showPrice ?? 0.00;
    } else if (good.showPriceType == 2) {
      //showPriceType==2会员价(showPrice为零售价，price为会员价)
      price = good.showPrice ?? 0.00;
      knightPrice = good.price ?? 0.00;
    }
    var privatePrice = getPrivatePrice(Decimal.parse(knightPrice.toString()).toString());

    if (good.promotionType! > 0 && good.promotionPrice! > 0) {
      this.price = "¥${Decimal.parse(good.promotionPrice.toString()).toString()}";
      //展示价格类型0(零售价与会员价相等) 没有会员价
      if (good.showPriceType! == 0) {
        showLabel = false;
        //判断活动价格和零售价格的大小
        if (good.promotionPrice < price) {
          //活动价格小于零售价 则显示零售价为划线价
          showLine = true;
          orgPrice = '¥$price';
        } else {
          // 零售价大于活动价的时候 ，再判断市场价 与 活动价的大小
          if (good.promotionPrice < good.marketPrice) {
            // 活动价格小于市场价则 市场价为划线价
            showLine = true;
            orgPrice = '¥${good.marketPrice!}';
          } else {
            orgPrice = '';
          }
        }
      } else {
        // 会员价与零售价不相等的情况下，判断会员价格与活动价的大小
        if (knightPrice < good.promotionPrice) {
          // 会员价小于活动价 则 需要判断是否是会员，
          if (isKnight) {
            // 是会员则直接显示会员价格
            showLine = false;
            orgPrice = '¥$knightPrice';
          } else {
            // 非会员 则显示 隐私数据
            orgPrice = '¥$privatePrice';
          }
        } else {
          // 活动价格大于会员价 则 需要隐藏会员价格的标签
          showLabel = false;
          // 这个时候又需要 判断
          //判断活动价格和零售价格的大小
          if (good.promotionPrice < price) {
            //活动价格小于零售价 则显示零售价为划线价
            showLine = true;
            orgPrice = '¥$price';
          } else {
            // 零售价大于活动价的时候 ，再判断市场价 与 活动价的大小
            if (good.promotionPrice < good.marketPrice) {
              // 活动价格小于市场价则 市场价为划线价
              showLine = true;
              orgPrice = '¥${good.marketPrice}';
            } else {
              orgPrice = '';
            }
          }
        }
      }
    } else {
      this.price = "¥${Decimal.parse(good.price.toString()).toString()}";
      //展示价格类型0(零售价与会员价相等) 没有会员价
      if (good.showPriceType == 0) {
        showLabel = false;
        // 判断市场价与 零售价的大小
        if (price < good.marketPrice ) {
          // 零售价小于市场价 则 划线价显示市场价
          showLine = true;
          orgPrice = '¥${good.marketPrice}';
        } else {
          orgPrice = '';
        }
      } else {
        //  有会员价的时候 判断会员价和零售价
        if (knightPrice < price) {
          showLabel = true;
          if (isKnight) {
            orgPrice = '¥$knightPrice';
          } else {
            orgPrice = '¥$privatePrice';
          }
        } else {
          if (price < good.marketPrice!) {
            // 零售价小于市场价 则 划线价显示市场价
            showLine = true;
            orgPrice = '¥${good.marketPrice}';
          } else {
            orgPrice = '';
          }
        }
      }
    }
  }

  String getPrivatePrice(String knightPrice) {
    var memberPrice = knightPrice;
    var price = StringBuffer();
    for (var i = 0; i < memberPrice.length; i++) {
      if (knightPrice.length > 5) {
        if (i >= 3) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else if (knightPrice.length > 3) {
        if (i >= 2) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else {
        if (i >= 1) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      }
    }
    return price.toString();
  }
}
